#include "functions.h"

extern int qMatrix [64];
extern unsigned char Code [32];
extern int blockSize;
extern float threshold;
extern float compressionScale;
extern int charsPerBlock;
extern int nBitsCode;
extern bool compressionJPG;
extern float sigmaNoise;
extern int quality;
extern string xorMask;

int main(int argc, char *argv[]) {
	const char* path_img_in = cimg_option("-i",(char*)"../images/esteg.bmp","Ruta y nombre de la imagen a retirar info.");
	CImg<float> eImg (path_img_in); //imagen a desesteganografiar

	loadConfig();
	unsigned int w = eImg.width(), h = eImg.height();
	CImgList<> bases (blockSize*blockSize,blockSize,blockSize);
	CImg<float> oImg(eImg); // imagen original
	float E; 	// energía de cada bloque

	vector<float> vZig; 	// vector ordenado por zigzag
	vZig.resize(blockSize*blockSize);
	CImg<float> qMat(qMatrix,blockSize,blockSize,1,1); // levantamos la matriz de cuantificacion de Losheller
	qMat /= compressionScale;
	makeBase(bases); // armo las bases de coseno
	vector<char> pChar;

	//Armamos cada bloque de 8x8.
	int entran = 0;

	char* cad = loadText("text.txt");

	for(unsigned int i=0;i<w-(w%8);i+=blockSize){
		for(unsigned int j=0;j<h-(h%8);j+=blockSize){
			CImg<float> block(blockSize,blockSize),dctBlock(blockSize,blockSize);
			cimg_forXY(block,x,y){
				block(x,y) = eImg(i+x,j+y);
			}
			cimg_forXY(block,x,y){
				block(x,y) -= 128;
			}
			dctBlock = DCT(block,bases,blockSize);
			E = norm2(dctBlock);
			//cout<<"E: "<<E<<"  - i:"<<entran<<endl;
			//si la subimagen tiene la suficiente energia, extraigo de ella algun caracter
			if(th(E)){
				//dctBlock = dctBlock / qMat; no funca
				cimg_forXY(dctBlock,x,y){
					dctBlock(x,y) = dctBlock(x,y)/qMat(x,y);
				}
				zigZag(dctBlock,vZig,false);
				LSBInv(vZig, pChar);
				//if(pChar[entran] != cad[entran]) printf("Error: %f .\n",E);
				entran ++;
			}
		}
	}
	//cout << "La cadena es: "<< &pChar[0]<< endl << endl;
	//encriptyon(&pChar[0]);
	cout << "La cadena Restaurada es: "<< endl;
	for(unsigned int i=0;i<pChar.size();i++)
		cout<<pChar[i];
	cout<<endl;

	return 0;
}

